package com.shareppy.norman.conversion;

import java.util.List;
import java.util.Map;

import org.json.JSONArray;

import com.shareppy.norman.core.entity.AbstractEntity;
import com.shareppy.utils.conversion.Converter;

public class EntityListJsonConverter<AE extends AbstractEntity> implements
		Converter<List<AE>, JSONArray> {
	
	private Map<String, String> keys;

	public EntityListJsonConverter() {
		this(null);
	}
	
	public EntityListJsonConverter(Map<String, String> keys) {
		this.keys = keys;
	}

	@Override
	public JSONArray convert(List<AE> input) throws Exception {
		if( input == null ){
			return null;
		}
		JSONArray jsonArray = new JSONArray();
		EntityJsonConverter<AE> entityJsonConverter = new EntityJsonConverter<AE>(keys);
		for (AE ae : input) {
			jsonArray.put( entityJsonConverter.convert(ae) );
		}
		return jsonArray;
	}
}